<?php
$web_services_location = sprintf('http://%s/apicomm.backend/public/index.php/apicomm/web_services/', 
	                             $_SERVER['SERVER_NAME']);
define('WEB_SERVICES_LOCATION', $web_services_location);

class Controller_APIComm extends Controller
{	
	// Handle soap requests
	public function action_web_services()
	{	
		$soap = new SoapServer(null, array('uri' => 'http://apicomm.com/'));
		$soap->setClass('Model_WebServices');
		$soap->handle();
	}
	
	// Login
	public function action_login() 
	{		
		$username = $_GET['username'];
		$password = $_GET['password'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->login($username, $password);
				
		header("Content-Type:text/xml");
		echo $result;
	}
	
	// Logout
	public function action_logout()
	{	
		$id = $_GET['id'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->logout($id);
		
		header("Content-Type:text/xml");
		echo $result;
	}
	
	public function action_search_staff()
	{
		$session = $_GET['session'];
		$query   = $_GET['query'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->search_staff($session, $query);
		
		header("Content-Type:text/xml");
		echo $result;
		
		// var_dump(Model_WebServices::search_staff($session, $query));

		/* $view = self::create_view('staffs');
		$view->set_global('raw', simplexml_load_string($result), false);
		return $view; */
	}
	
	public function action_search_account()
	{
		$session = $_GET['session'];
		$query   = $_GET['query'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->search_account($session, $query);
		
		header("Content-Type:text/xml");
		echo $result;
		
		// var_dump(Model_WebServices::search_staff($session, $query));

		/* $view = self::create_view('staffs');
		$view->set_global('raw', simplexml_load_string($result), false);
		return $view; */
	}
	
	public function action_search_equipment()
	{
		$session = $_GET['session'];
		$query   = $_GET['query'];
		
		/* $client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->search_equipment($session, $query);
		
		header("Content-Type:text/xml");
		echo $result; */
		
		/* $result = Model_WebServices::add_account('03853c25-6cf7-cca1-6eec-313955b09079',
			'zxv', 'asdf', '12', 'asdf', 'asdf', '2012-05-09 13:37:58', 'PHL', 'asdf', 'asdf',
			'asdf', 'asdf', '1234', 'asdf', 'asdf', 'asdf', 'asdf', '1'); */
		
		$data = array();
		$data['equipment_id'] = 'equip_666';
		$data['name']         = 'schyte';
		$data['description']  = 'pink schyter';
		
		$result = Model_WebServices::add_equipment($session, $data);
		
		header("Content-Type:text/xml");
		echo $result;
	}
		
	/* private function create_view($content) {
		$view = View::forge('apicomm');
		
		$view->set_global('site_title', 'apicomm');
		
		$view->head    = View::forge('head');
		$view->header  = View::forge('header');
		$view->content = View::forge($content);
		$view->footer  = View::forge('footer');
		
		return $view;
	} */

	public function action_delete_account()
	{
		$session  = $_GET['session'];
		$username = $_GET['username'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->delete_account($session, $username);
		
		header("Content-Type:text/xml");
		echo $result;
	}
	
	public function action_delete_staff()
	{
		$session = $_GET['session'];
		$id      = $_GET['id'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->delete_staff($session, $id);
		
		header("Content-Type:text/xml");
		echo $result;
	}
	
	public function action_delete_equipment()
	{
		$session = $_GET['session'];
		$id      = $_GET['id'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->delete_equipment($session, $id);
		
		header("Content-Type:text/xml");
		echo $result;
	}

	public function action_update_account()
	{
		$session = $_GET['session'];
		
		$data                = array();
		$data['username']    = $_GET['username'];
		$data['password']    = $_GET['password'];
		$data['level']       = $_GET['level'];
		$data['employee_id'] = $_GET['employee_id'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->update_account($session, $data);
		
		header("Content-Type:text/xml");
		echo $result;
	}
	
	public function action_update_equipment()
	{
		$session = $_GET['session'];
		
		$data                 = array();
		$data['id']           = $_GET['id'];
		$data['equipment_id'] = $_GET['equipment_id'];
		$data['name']         = $_GET['name'];
		$data['description']  = $_GET['description'];
		$data['added']        = $_GET['added'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->update_equipment($session, $data);
		
		header("Content-Type:text/xml");
		echo $result;
	}
	
	public function action_update_staff()
	{
		$session = $_GET['session'];
		
		$data                   = array();
		$data['id']             = $_GET['id'];
		$data['employee_id']    = $_GET['employee_id'];
		$data['username']       = $_GET['username'];
		$data['first_name']     = $_GET['first_name'];
		$data['last_name']      = $_GET['last_name'];
		$data['gender']         = $_GET['gender'];
		$data['birthday']       = $_GET['birthday'];
		$data['country']        = $_GET['country'];
		$data['city']           = $_GET['city'];
		$data['street_address'] = $_GET['street_address'];
		$data['mobile']         = $_GET['mobile'];
		$data['schedule_days']  = $_GET['schedule_days'];
		$data['schedule_time']  = $_GET['schedule_time'];
		$data['role_id']        = $_GET['role_id'];
		
		$client = new SoapClient(null, array('location' => WEB_SERVICES_LOCATION,
											 'uri' => 'http://apicomm.com/'));		
		$result = $client->update_staff($session, $data);
		
		header("Content-Type:text/xml");
		echo $result;
	}
}
?>